<template>
  <el-dialog :visible.sync="visible"
             title="修改"
             :close-on-click-modal="false"
             :append-to-body="true"
             :before-close="closeDialog"
             class="add-or-updata"
             width="30%"
             :close-on-press-escape="false"
             v-if="visible">
    <el-form :model="dataForm"
             ref="dataForm"
             label-position="left"
             @keyup.enter.native="dataFormSubmitHandle()"
             label-width="106px">
      <el-form-item prop="boardName"
                    label="平台名称"
                    :rules="stringRule()">
        <el-input v-model.trim="dataForm.boardName"
                  disabled
                  placeholder=""></el-input>
      </el-form-item>
      <el-form-item prop="serviceAddress"
                    label="服务地址"
                    :rules="stringRule()">
        <el-input v-model.trim="dataForm.serviceAddress"
                  placeholder=""></el-input>
      </el-form-item>
      <el-form-item prop="appId"
                    label="APPID"
                    :rules="stringRule()">
        <el-input v-model.trim="dataForm.appId"
                  placeholder=""></el-input>
      </el-form-item>
      <el-form-item prop="interfaceAddress"
                    label="接口地址"
                    :rules="stringRule()">
        <el-input v-model.trim="dataForm.interfaceAddress"
                  placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="closeDialog">{{ $t('cancel') }}</el-button>
      <el-button type="primary"
                 :loading="isRequest"
                 @click="dataFormSubmitHandle()"
                 :disabled="enableConfig">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import formValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'

export default {
  mixins: [mixinViewModule, formValidate],
  components: {
  },
  data () {
    return {
      visible: false,
      isRequest: false,
      enableConfig: false, // 确认之后禁用掉按钮，等操作成功之后恢复
      mixinViewModuleOptions: {
        getDataInfoURL: '', // 获取角色管理的详情
        updateURL: '/tb/thirdPartyBoard/updateBoard'
      },
      dataForm: {
        id: '',
        interfaceAddress: '',
        appId: '',
        serviceAddress: ''
      }
    }
  },
  computed: {
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.isRequest = false
      this.$refs['dataForm'].resetFields()
    },
    init (data) {
      console.log(data)
      this.dataForm = data
      this.dataForm.id = data.id
      this.visible = true
      this.isRequest = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.isRequest = true
          const url = this.mixinViewModuleOptions.updateURL
          this.addOrUpdateItem({ url }).then(res => {
            if (res.code !== 0) {
              return
            }
            this.$emit('callBack')
            this.closeDialog()
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
</style>
